package demo;

import java.io.File;
import java.util.Calendar;

import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;
import org.artofsolving.jodconverter.process.SigarProcessManager;

public class JODConverter {
	
	private static JODConverter instance = null;
	
	private static OfficeManager officeManager = null;
	
	public static JODConverter getInstance(){
		if (instance==null || officeManager==null){
			Long begin=Calendar.getInstance().getTimeInMillis();//LOG
			officeManager = new DefaultOfficeManagerConfiguration().buildOfficeManager();
			Long end=Calendar.getInstance().getTimeInMillis();//LOG			
			System.out.println("Build Office Manager Take millisseconds:"+ (end-begin));//LOG
			instance = new JODConverter();
		}
		return instance;
	}
	
	/**
	 * <b>Caution: In order to use to replace default process manager, more
	 * configuration required for JVM.</b><br/>
	 * OOo inter-process communication can use either TCP sockets or named
	 * pipes. The default is to use a TCP socket, on port 2002.<br>
	 * 
	 * Named pipes have the advantage of not taking up TCP ports (with their
	 * potential security implications), and they are marginally faster. However
	 * they require a native library to be loaded by the JVM, and this means
	 * having to set the java.library.path system property. That's why it's not
	 * the default.<br/>
	 * 
	 * The path that needs to be added to java.library.path is different
	 * depending on the platform, but it should be the directory in the OOo
	 * installation containing libjpipe. On Linux it's e.g.:<br/>
	 * 
	 * java -Djava.library.path=/opt/openoffice.org/ure/lib ...<br/>
	 * while on Windows:<br/>
	 * 
	 * java "-Djava.library.path=C:\Program Files\OpenOffice.org 3\URE\bin"
	 * ...</br>
	 */
	public void setSigarProcessManager() {
		officeManager = new DefaultOfficeManagerConfiguration()
				.setProcessManager(new SigarProcessManager())
				.buildOfficeManager();
	}
	
	/**
	 * All conversions supported by OpenOffice.org, including<br/>
	 * 
	 * Microsoft Office to OpenDocument, and viceversa<br/>
	 * Word to OpenDocument Text (odt); OpenDocument Text (odt) to Word<br/>
	 * Excel to OpenDocument Spreadsheet (ods); OpenDocument Spreadsheet (ods)
	 * to Excel<br/>
	 * PowerPoint to OpenDocument Presentation (odp); OpenDocument Presentation
	 * (odp) to PowerPoint<br/>
	 * 
	 * Any format to PDF<br/>
	 * OpenDocument (Text, Spreadsheet, Presentation) to PDF<br/>
	 * Word to PDF; Excel to PDF; PowerPoint to PDF<br/>
	 * RTF to PDF; WordPerfect to PDF; ...<br/>
	 * And more<br/>
	 * 
	 * OpenDocument Presentation (odp) to Flash; PowerPoint to Flash<br/>
	 * RTF to OpenDocument; WordPerfect to OpenDocument<br/>
	 * Any format to HTML (with limitations)<br/>
	 * Support for OpenOffice.org 1.0 and old StarOffice formats<br/>
	 * 
	 * @param source
	 * @param destination
	 */
	public void convertDocumentToPDF(File source, File destination){
		try{
			Long begin=Calendar.getInstance().getTimeInMillis();//LOG
			officeManager.start();
			Long end=Calendar.getInstance().getTimeInMillis();//LOG			
			System.out.println("Office Manager Start sTake millisseconds:"+ (end-begin));//LOG
			
		    OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
		    Long end2=Calendar.getInstance().getTimeInMillis();//LOG			
		    System.out.println("New OfficeDocumentConverter Take millisseconds:"+ (end2-end));//LOG
		    
		    converter.convert(source, destination);
		    Long end3=Calendar.getInstance().getTimeInMillis();//LOG			
		    System.out.println("Convert Take millisseconds:"+ (end3-end2));//LOG
		    
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			officeManager.stop();
		}
	}
}
